package org.blogwu.learn.designpattern.chapter16.v2;

public class PrintProxy extends Printable {

    private String name;

    private Printer read;

    public PrintProxy() {
    }

    public PrintProxy(String name) {
        this.name = name;
    }

    @Override
    public void setPrintName(String name) {
        if (read != null) {
            // 同时设置本人名字
            read.setPrintName(name);
        }
    }

    @Override
    public String getPrinterName() {
        return name;
    }

    @Override
    public void print(String string) {
        readlize();
        read.print(string);
    }

    private synchronized void readlize() {
        if (read == null) {
            read = new Printer(name);
        }
    }
}
